<div class='division'>
<h3><{$title}></h3>
<form method='post' action='index.php?app=inventorydepth&ctl=regulation_apply&act=save' id='regulation-apply-form'>
    <input type='hidden' name='init_bn' value="<{$data.bn|default:$init_bn}>">
    <input type='hidden' name='id' value="<{$data.id|default:''}>">
    <div class='tableform'>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <th><span class="red">*</span> 应用编号:</th>
                    <td><input type='text' name='bn' value="<{$data.bn|default:''}>" vtype='required' class='x-input' <{if $data.bn}>readonly='readonly'<{/if}> ></td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 应用名称:</th>
                    <td><{input type='text' name='heading' value=$data.heading vtype='required'}></td>
                </tr>
                <tr id='condition-tr'>
                    <th><span class="red">*</span> 规则类型:</th>
                    <td>
                        <{if $data.condition=='stock'}> 更新销售库存 <{elseif $data.condition=='frame'}> 更新商品上下架状态 <{/if}>
                        <input type='hidden' name='condition' value="<{$data.condition}>">
                    </td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 触发类型:</th>
                    <td><{input type="select" required=true name="style" options=$options.style value=$data.style}></td>
                </tr>
                <tr id='apply-range'>
                    <th><span class="red">*</span> 应用范围:</th>
                    <td>
                    <{*
                    <input type='radio' name='rangetype' value='all' <{if $data.shop_id.0 == '_ALL_'}>checked<{/if}>>所有店铺
                    <input type='radio' name='rangetype' value='some' <{if $data.shop_id.0 != '_ALL_'}>checked<{/if}>>指定店铺
                    <input type='hidden' vtype='requiredcheckbox' />
                    *}>
                    <ul <{if $data.shop_id.0 == '_ALL_'}>style='display:none;'<{/if}> id='shops'>
                        <{foreach from=$data.shops item=shop}>
                            <li style='float:left;'><input type='checkbox' name='shop_id[]' value='<{$shop.shop_id}>' <{if in_array($shop.shop_id,$data.shop_id)}>checked<{/if}>><{$shop.name}></li>
                        <{/foreach}>
                        <div class='clear'></div>
                    </ul>
                    <{*
                    <{input type='object' object='shop@ome' multiple=true breakpoint='0'  name='shop_id' value=$data.shop_id}>
                    *}>
                    </td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 应用对象:</th>
                    <td>
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr class='choose' id='choose_frame' <{if $data.condition!='frame'}>style='display:none;'<{/if}>>
                                    <td>
                                        <div style='margin-bottom:5px;'><input type='checkbox' name='apply-goods-all' value='true' onclick="isAll(this);" <{if $data.apply_goods.id == '_ALL_'}>checked<{/if}> >全部商品</div>
                                        <div style="display:inline-block;*zoom:1;" class="object-select clearfix ids">
                                          <div rel="请选择商品集..." id="" class="label">请选择商品集...</div>
                                          <div class="handle">&nbsp;</div>
                                          <input type="hidden" value="" vtype="" name="">
                                        </div>
                                        <div style="display:inline-block;*zoom:1;" class="object-select clearfix filters">
                                          <div rel="请选择商品集条件..." id="" class="label">请选择商品集条件...</div>
                                          <div class="handle">&nbsp;</div>
                                        </div>
										<div class='gridlist rows-body'></div>
                                    </td>
                                </tr>
                                <tr class='choose' id='choose_stock' <{if $data.condition!='stock'}>style='display:none;'<{/if}>>
                                    <td>
                                        <div style='margin-bottom:5px;'><input type='checkbox' name='apply-goods-all' value='true' onclick="isAll(this);" <{if $data.apply_goods.id == '_ALL_'}>checked<{/if}> >全部货品</div>
                                        <div style="display:inline-block;*zoom:1;" class="object-select clearfix ids">
                                          <div rel="请选择货品集..." id="" class="label">请选择货品集...</div>
                                          <div class="handle">&nbsp;</div>
                                          <input type="hidden" value="" vtype="" name="">
                                        </div>
                                        <div style="display:inline-block;*zoom:1;" class="object-select clearfix filters">
                                          <div rel="请选择货品集条件..." id="" class="label">请选择货品集条件...</div>
                                          <div class="handle">&nbsp;</div>
                                        </div>
										<div class='gridlist rows-body'></div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 应用规则:</th>
                    <td><{input type='object' object='regulation' multiple=false breakpoint='0' name='regulation_id' value=$data.regulation_id filter="using=true&condition={$data.condition}" }></td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 开始时间:</th>
                    <td><{input type='time' name='start_time' value=$data.start_time vtype='required'}></td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 结束时间:</th>
                    <td><{input type='time' name='end_time' value=$data.end_time vtype='required'}></td>
                </tr>
                <tr>
                    <th>规则应用等级</th>
                    <td><{input type='text' vtype='unsignedint' name='priority' value=$data.priority|default:99 size=8}></td>
                </tr>
                <tr>
                    <th><span class="red">*</span> 应用启用状态 :</th>
                    <td><{if $data.using=='true'}>启用<{else}>停用<{/if}></td>
                </tr>
            </tbody>
        </table>
    </div>

</form>
</div>

<{capture name='footbar'}>
    <div class='table-action'>
        <{button type='button' label='确认' id='regulation-apply-submit'}>
        <{button type='button' label='取消' onclick='javascript:window.close();'}>
    </div>
<{/capture}>
<script>
function isAll(el){
    if (el.checked) {
        el.getParent('td').getElements('.object-select').setStyle('display','none');
    }else{
        el.getParent('td').getElements('.object-select').setStyle('display','inline-block');
    }
}
void function(){

    $('regulation-apply-submit').addEvent('click',function(){
        var form = $('regulation-apply-form');

        W.page(form.action,{
            data:form,
            onComplete:function(resp){
                resp = JSON.decode(resp);
                if (resp.error)
                {
                    MessageBox.error(resp.error);
                    return ;
                }

                opener.finderGroup["<{$env.get._finder.finder_id}>"].refresh.delay(400,opener.finderGroup["<{$env.get._finder.finder_id}>"]);
                setTimeout("window.close()",200);
            }
        });
    });

    $ES('#regulation-apply-form #apply-range input[name="rangetype"]').addEvent('click',function(){
            if (this.value=='all')
            {
                $E('#regulation-apply-form #apply-range #shops').hide();
            }else{
                $E('#regulation-apply-form #apply-range #shops').show();
            }
    });

    // $ES('#regulation-apply-form #condition-tr input[type="radio"]').addEvent('click',function(){
    //     $ES('#regulation-apply-form .choose').hide();
    //     $ES('#regulation-apply-form .choose input').set('disabled',true);
    //     $E('#regulation-apply-form #'+this.get('choose')).show();
    //     $ES('#regulation-apply-form #'+this.get('choose')+ ' input').set('disabled',false);
    // });
    
    show_object_select();
    function show_object_select(){
        var id = "<{$data.apply_goods.id}>";
        var condition = "<{$data.condition}>";
        if (id=='_ALL_') {
            if (condition=='stock')
            {
                var _handle = $E('#choose_stock');
            }else{
                var _handle = $E('#choose_frame');
            }

            _handle.getElements('.object-select').setStyle('display','none');
        };
    }

    init_apply_goods();
    function init_apply_goods(){
        var init_bn = "<{$init_bn}>";
        var condition = "<{$data.condition}>";
        var merchandise_id = "<{$data.apply_goods.id}>";
        if (!merchandise_id || merchandise_id=='_ALL_') {return};

        if (condition=='stock')
        {
            var _handle = $E('#choose_stock .gridlist');
        }else{
            var _handle = $E('#choose_frame .gridlist');
        }
        var data = "<{$apply_goods_query}>";
        if (!data)
        {
            return ;
        }
        data += '&condition='+condition+'&init_bn='+init_bn;
        new Request({
            url:'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_filter',
            onComplete:function(resp){
                resp = JSON.decode(resp);
                if (resp.error) {
                    MessageBox.error(resp.error);
                    return;
                }
                _handle.setHTML(resp.success);
            }
        }).send(data);
    }

    init_apply_filter();
    function init_apply_filter(){
        var init_bn = "<{$init_bn}>";
        var condition = "<{$data.condition}>";
        var merchandise_id = "<{$data.apply_goods.id}>";
        var apply_goods = "<{$data.apply_goods}>";
        if (merchandise_id || !apply_goods) {return};

        if (condition=='stock')
        {
            var _handle = $E('#choose_stock .gridlist');
        }else{
            var _handle = $E('#choose_frame .gridlist');
        }
        var data = "<{$apply_goods_query}>";
        data = data.replace(/(&amp;)/g,',');
        data = data.replace(/(&)/g,',');

        data = 'filter[advance]='+data;
        if (!data)
        {
            return ;
        }
        data += '&condition='+condition+'&init_bn='+init_bn;
        new Request({
            url:'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_filter_array',
            onComplete:function(resp){
                resp = JSON.decode(resp);
                if (resp.error) {
                    MessageBox.error(resp.error);
                    return;
                }
                _handle.setHTML(resp.success);
            }
        }).send(data);

    }
    $ES('#regulation-apply-form .choose .filters').addEvent('click',function(){
        var t_url = 'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_dialog_filter';
        //var condition = $E('#regulation-apply-form #condition-tr input[type="radio"]:checked').get('value');
        var condition = $E('#regulation-apply-form #condition-tr input[name="condition"]').get('value');
        var init_bn = $E('#regulation-apply-form input[name="init_bn"]').get('value');

        t_url += '&condition='+condition;

        var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent(t_url);

        var _handle = this.getNext('.gridlist');
        //new finderDialog(url);
        Ex_Loader('modedialog',function(){
            filterMDialog = new Class({
                Extends:ModeDialog,
                options:{
                    onHide:function(value){
                        var params=this.options.params;
                        var data = params.postdata;
                        data += value ? '&filter[advance]='+value : '';
                        new Request({url:params.url,onSuccess:function(rs){
                             this.fireEvent('callback',rs);
                        }.bind(this)}).send(data);
                    }
                },
                submit:function(win){
                    var f = this.doc.getElement('div[id^=filter-list-]');
                    var qstr = f.toQueryString(
                        function(el) {
                            var elp = $(el).getParent('dl'),m;

                            if(!elp||!elp.isDisplay()||!!!$(el).value)return;

                            if(m = el.name.match(/_([\s\S]+)_search/)){
                                if(!!!f.getElement('*[name='+m[1]+']').value)return;
                            }

                            if(el.name.match(/_DTYPE_TIME/)){
                                if(!!!f.getElement('*[name='+el.value+']').value)return;
                            }

                            if(m=el.name.match(/_DTIME_\[([^\]]+)\]\[([^\]]+)\]/)){
                                if(!!!f.getElement('*[name='+m[2]+']').value)return;
                            }
                            return true;
                    },true);
                    qstr = qstr.replace(/(&amp;)/g,',');
                    qstr = qstr.replace(/(&)/g,',');
                    win.returnValue=qstr;
                }
            });
            new filterMDialog(url,{
                params: {
                    url: 'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_filter_array',
                    postdata:'condition='+condition+'&init_bn='+init_bn
                },
                onCallback: function(data) {
                
                    data = JSON.decode(data);
                    if (data.error) {
                        MessageBox.error(data.error);
                        return;
                    }
                    _handle.setHTML(data.success);
                    
                }
            });
        });
    });

    $ES('#regulation-apply-form .choose .ids').addEvent('click',function(){
		var _handle = this.getNext('.gridlist');
        Ex_Loader('modedialog',function(){
            idmFinderDialog=new Class({
                Extends:finderDialog,
                options:{
                    onHide:function(value){
                        if(!value||!value.length)return;
                        var tmpForm=new Element('div'),fdoc=document.createDocumentFragment();
                        var params=this.options.params;
                        for(var i=0,l=value.length;i<l;i++){
                            fdoc.appendChild(new Element('input',{type:'hidden','name':params.name,value:value[i]}));
                        }
                        tmpForm.appendChild(fdoc);
                        var data=(params.postdata)?tmpForm.toQueryString()+'&'+params.postdata:tmpForm.toQueryString();
                        data = this.filterData?data+'&filter[advance]='+this.filterData:data;
                        new Request({url:params.url,onSuccess:function(rs){
                             tmpForm.destroy();
                             if(params.type)this.options.select(params,rs,value);
                             this.fireEvent('callback',rs);
                        }.bind(this)}).send(data);
                    }
                },
                submit:function(win){
                    var form=this.doc.getElement('form[id^=finder-form-]');
                    var value=form.retrieve('rowselected','');
                    var data = decodeURI(form.getElement('input[id^=finder-filter-]').value);
                    var params = this.options.params;
                    if(params.app&&data&&value=='_ALL_') {
                        data = data.replace(/(&amp;)/g,',');
                        data = data.replace(/(&)/g,',');
                       this.filterData = data;
                    }

                    if(!value || !value.length) return;
                    win.returnValue=value.toString().split(',');
                }
            });

            var init_bn = $E('#regulation-apply-form input[name="init_bn"]').get('value');
            //var condition = $E('#regulation-apply-form #condition-tr input[type="radio"]:checked').get('value');
            var condition = $E('#regulation-apply-form #condition-tr input[name="condition"]').get('value');
            var shop_id = '';
            var rangetype = $ES('#regulation-apply-form #apply-range input[name="rangetype"]:checked');
            
            if (rangetype.get('value') == 'all')
            {
                shop_id = 'shop[0]=_ALL_';
            }else if(rangetype.get('value') == 'some'){
                shop_id = $E('#regulation-apply-form #apply-range ul').toQueryString();
            }

            var t_url = 'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_finder';
                 t_url += '&condition='+condition;
                 t_url += '&'+shop_id;
                 
            var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent(t_url);
            
            new idmFinderDialog(url, {
                params: {
                    url: 'index.php?app=inventorydepth&ctl=regulation_apply&act=merchandise_filter',
                    name:'id[]',
                    app:'true',
                    postdata:'condition='+condition+'&init_bn='+init_bn+'&'+shop_id
                },
                onCallback: function(data) {
                    data = JSON.decode(data);
                    if (data.error) {
                        MessageBox.error(data.error);
                        return;
                    }
					_handle.setHTML(data.success);
                }
            });
          });
    });
}();
</script>